﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellThreadPoolExecutor_h
#define CellThreadPoolExecutor_h

#include "CellPrerequisites.h"
#include "CellExecutorService.h"
#include "CellThread.h"
#include "CellFuture.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

class ExecutorTask;

/** 线程池执行器。
@author
	Jiangwei Xu
*/
class _CellExport ThreadPoolExecutor : public ExecutorService
{
public:
	/**
	*/
	ThreadPoolExecutor(const String& name, uint32 maximumPoolSize);

	/**
	*/
	ThreadPoolExecutor(const String& name, uint32 corePoolSize, uint32 maximumPoolSize);

	/**
	*/
	virtual ~ThreadPoolExecutor(void);

public:
	/**
	*/
	inline uint32 getCorePoolSize(void) { return mCorePoolSize; }

	/**
	*/
	uint32 getMaxPoolSize(void);

	/**
	*/
	size_t getTaskListSize(void);

	/**
	*/
	size_t getActiveCount(void);

	/**
	*/
	size_t getIdleCount(void);

	/**
	*/
	size_t getTaskCount(void);

	/**
	*/
	void addListener(ExecutorListener* listener);

	/**
	*/
	void removeListener(ExecutorListener* listener);

	/**
	*/
	void remove(Runnable* task);

	/**
	*/
	virtual bool execute(Runnable* task);

	/**
	*/
	virtual void awaitExecution(long timeout = -1, uint8 unit = TimeUnit::MILLISECONDS);

protected:
	/**
	*/
	virtual void beforeExecute(Runnable* task) = 0;

	/**
	*/
	virtual void afterExecute(Runnable* task) = 0;

	/**
	*/
	bool resetCoreSize(uint32 size);

	/**
	*/
	bool resetMaxSize(uint32 size);

	/**
	*/
	bool isActiveTask(Runnable* task);

private:
	/**
	*/
	void construct(uint32 corePoolSize, uint32 maximumPoolSize);

	/**
	*/
	void warmup(void);

	/**
	*/
	bool knock(void);

	/**
	*/
	void discard(ExecutorTask* task);

	/**
	*/
	void fireBefore(ExecutorTask* task);

	/**
	*/
	void fireAfter(ExecutorTask* task);

	/**
	*/
	Runnable* apply(void);

protected:
	typedef std::list<Runnable*> TaskTable;
	typedef std::vector<ExecutorTask*> TaskList;

	uint32 mCorePoolSize;
	uint32 mMaximumPoolSize;

	TaskTable mTaskTable;

	TaskList mTaskList;
	TaskList mActiveTaskList;
	TaskList mIdleTaskList;
	TaskList mExpiredTaskList;

	typedef std::vector<ExecutorListener*> ListenerList;
	ListenerList mListenerList;

	CELL_MUTEX(mMutex);
	CELL_MUTEX(mListenerMutex);

	CELL_THREAD_SYNCHRONISER(mNewTask);

	friend class ExecutorTask;
};


/**
*/
class _CellPrivate ExecutorTask : public Thread
{
public:
	/**
	*/
	ExecutorTask(ThreadPoolExecutor* executor, bool spinning);

	/**
	*/
	virtual ~ExecutorTask(void);

public:
	/**
	*/
	void run(void);

	/**
	*/
	void kill(void);

	/**
	*/
	inline bool isRunning(void) const { return mRunning; }

	/**
	*/
	inline bool hasWorked(void) const { return mWorked; }

	/**
	*/
	inline void setWorker(Runnable* worker) { mWorker = worker; }

	/**
	*/
	inline Runnable* getWorker(void) const { return mWorker; }

public:
	uint64 timestamp;

private:
	ThreadPoolExecutor* mExecutor;
	bool mSpinning;
	Runnable* mWorker;
	bool mRunning;
	bool mKill;
	bool mWorked;

	CELL_MUTEX(mTaskMutex);
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellThreadPoolExecutor_h
